import Vue from "vue";
import Router from "vue-router";
import Main from "./views/layout/main.vue";

Vue.use(Router);

// 重定向
const redirect = {
    path: "/",
    redirect: "/phoneHome"
};
// 登录
const loginRoute = {
    path: "/login",
    name: "login",
    component: () => import("./views/pages/login/login.vue"),
    meta: {
        title: "login",
        author: false
    }
};
// 找回密码
const findPWDRoute = {
    path: "/find-pwd",
    name: "find-pwd",
    component: () => import("./views/pages/login/find-pwd.vue"),
    meta: {
        title: "login",
        author: false
    }
};
// 手机
const phoneRoute = {
    path: "/Phone",
    name: "Phone",
    component: () => import("./views/pages/Phone.vue"),
    meta: {
        title: "app下载",
        author: false
    }
};

/*********************** 公共H5页面开始***************************************** */

const phoneHomeRoute = {
    path: "/phoneHome",
    name: "phoneHome",
    component: () => import("./views/pages/custom/iphone/phoneHome.vue"),
    meta: {
        title: "app下载",
        author: false
    }
};

/*********************** 公共H5页面结束***************************************** */

// const notFound = {
//     path: "*",
//     redirect: "/404"
//     // hidden: true
// };

/*************  业务路由写在这个里面  **************** */

const mainRoutes = {
    path: "/main",
    component: Main,
    name: "main",
    children: []
};

/************  admin路由写在这个里面  *************** */
const adminRoutes = {
    path: "/main",
    component: Main,
    name: "main",
    children: [
        {
            path: "/SystemManagement_menu",
            name: "SystemManagement_menu",
            component: () =>
                import("./views/pages/system/system-menu-management.vue"),
            meta: {
                title: "菜单管理",
                author: true
            }
        },
        {
            path: "/SystemManagement_role",
            name: "SystemManagement_role",
            component: () =>
                import("./views/pages/system/system-role-management.vue"),
            meta: {
                title: "角色管理",
                author: true
            }
        },

        {
            path: "/SystemManagement_institutions",
            name: "SystemManagement_institutions",
            component: () =>
                import(
                    "./views/pages/system/system-institutions-management.vue"
                ),
            meta: {
                title: "机构管理",
                author: true
            }
        },
        // 404页面一定要放在醉下面
        {
            path: "/*",
            // name: "404",
            component: () => import("@/views/pages/system/404.vue"),
            meta: {
                title: "404",
                author: true
                // showTab: false // 是否显示再tab上
            }
        }
    ]
};

export const routerControl = async routes => {
    const assignRoutes = await assignRouter(formatFlatteningRoutes(routes));
};

export function formatFlatteningRoutes(arr) {
    if (arr.length <= 0) return false;
    for (let i = 0; i < arr.length; i++) {
        if (arr[i].childlist) {
            arr = arr
                .slice(0, i + 1)
                .concat(arr[i].childlist, arr.slice(i + 1));
        }
    }
    return arr;
}

// 整合路由，将后台返回的meta属性赋值给本地的路由meta权限
const assignRouter = async backRoutes => {
    return (
        mainRoutes.children &&
        mainRoutes.children.map(item => {
            const BackItem = backRoutes.filter(k => k.route_path == item.path);
            if (BackItem.length > 0) {
                item.meta["title"] = BackItem[0].menu_name;

                item.childlist && assignRouter(backRoutes);
                return item;
            }
        })
    );
};

const staticRoute = [
    redirect,
    loginRoute,
    findPWDRoute,
    phoneRoute,
    mainRoutes,
    adminRoutes //adminRoutes必须放在最后，因为最后一个是404页面
];

const router = new Router({
    mode: "history",
    // 跳转路由，重新设置屏幕滚动
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition;
        } else {
            return {
                x: 0,
                y: 0
            };
        }
    },
    routes: staticRoute
});

// 登录验证
router.beforeEach((to, from, next) => {
    const token = sessionStorage.getItem("token");
    if (window._axiosPromiseArr && window._axiosPromiseArr.length) {
        window._axiosPromiseArr.forEach((ele, index) => {
            ele.cancel();
            delete window._axiosPromiseArr[index];
        });
    }

    if (to.meta.author) {
        if (token) {
            next();
        } else {
            // 登录
            // if (to.path == "/login") {
            //     next();
            // } else {
            //     next("./login");
            // }
        }
    } else {
        next();
    }
});
export default router;
